import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:顺序表
 * User: mac
 * Date: 2022-08-30
 * Time: 20:41
 */
public class MyArrayList {
    public int[] elem;
    public int usedSize;//有效数据个数

    public MyArrayList() {
        this.elem = new int[2];
    }

    //打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    //获取顺序表有效数据长度
    public int size() {
        return this.usedSize;
    }

    //在pos位置新增元素
    public void add(int pos, int data) {
        if (pos < 0 || pos > this.usedSize){
            System.out.println("pos位置不合法");
            return;
        }
        if (isFull()){
           this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
            System.out.println("空间不足已扩容成功");
        }
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    //判断满
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    //判断是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //获取pos位置的值
    public int getPos(int pos) {
        if (pos < 0 || pos >= this.usedSize) {
            System.out.println("pos位置不合法");
            return -1;//业务上的处理不考虑这些，可以抛异常解决
        }
        if (isEmpty()) {
            System.out.println("顺序表为空！");
            return -1;
        }
        return this.elem[pos];
    }

    //判空
    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    //给pos位置的元素设置为value
    public void setPos(int pos, int value) {
        if (pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合法");
            return;
        }
        if (isEmpty()){
            System.out.println("顺序表为空");
            return;
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字
    public void remove(int toRemove) {
        if (isEmpty()) {
            System.out.println("顺序表为空！");
            return;
        }
        int index = search(toRemove);
        if (index == -1){
            System.out.println("没有要删除的数字");
            return;
        }
        for (int i = index; i < this.usedSize - 1; i++) {
            this.elem[i] = this.elem[i + 1];
        }
        this.usedSize--;
        //this.elem[usedSize] = null;如果数组当中是引用数据类型
    }

    //清空顺序表
    public void clear() {
        this.usedSize = 0;
        //如果为引用类型
        /*for (int i = 0; i < usedSize; i++) {
            this.elem[i] = null;
        }*/
    }
}
